package com.wenc.psi.rvAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wenc.psi.R;
import com.wenc.psi.pojo.Customer;
import com.wenc.psi.pojo.Part;

import java.util.List;

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.CustomerViewHolder>{
    private List<Customer> customerList;

    public void setCustomerList(List<Customer> customerList) {
        this.customerList = customerList;
    }

    @NonNull
    @Override
    public CustomerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View itemView = layoutInflater.inflate(R.layout.cell_customer, parent, false);
        return new CustomerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomerViewHolder holder, int position) {
        Customer customer = customerList.get(position);
        holder.tvCustomerId.setText(String.valueOf(customer.getId()));
        holder.tvCustomerName.setText(String.valueOf(customer.getName()));
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemChoiceClick(customer);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (customerList != null) return customerList.size();
        return 0;
    }

    class CustomerViewHolder extends RecyclerView.ViewHolder{
        private TextView tvCustomerId, tvCustomerName;
        public CustomerViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCustomerId = itemView.findViewById(R.id.customer_id);
            tvCustomerName = itemView.findViewById(R.id.customer_name);
        }
    }

    public interface OnItemClickListener{
        void onItemChoiceClick(Customer customer);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
